﻿<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
<meta content="yes" name="apple-mobile-web-app-capable">
<meta content="black" name="apple-mobile-web-app-status-bar-style">
<meta content="telephone=no" name="format-detection">
<link rel="stylesheet" type="text/css" href="../static/easyui/themes/bootstrap/easyui.css">
<link rel="stylesheet" type="text/css" href="../static/easyui/themes/icon.css">
<link rel="stylesheet" href="../static/css/style-app.css" />
<script src="../static/jquery/jquery-1.12.4.js"></script>
<script src="../static/easyui/jquery.easyui.min.js"></script>
<script src="../static/easyui/locale/easyui-lang-zh_CN.js"></script>
<title>工程联系单</title>
</head>

<body>
	<div class="top-part">
		<div class="header">
			<div class="return">
				<a href="javascript:history.go(-1);"></a>
			</div>
			<div class="header-title">工程联系单管理</div>
		</div>
	</div>
	<div class="new-form">
		<div>
			<form id="ff" method="post">
				<table cellpadding="5" style="margin: auto;">
					<tr>
						<td style="width: 100px;">项目:</td>
						<td id="width1">
							<input style="width: 100%;" id="projectitem" name="projectitem" class="easyui-combobox"
								editable="false"
								data-options="
							    valueField: 'id',
							    textField: 'name',
							    url: '../projectitemlist.do?status=2',
							    onSelect: function(rec){
								    if(rec.projectitemchilds.length>0){
										document.getElementById('tr').style.display = '';
										//document.getElementById('tr2').style.display = '';
										var url = '../projectitemchildlist?status=2&&projectitemid=' + rec.id;
										$('#projectitemchild').combobox('reload', url);
								    }else{
										document.getElementById('tr').style.display = 'none';
										//document.getElementById('tr2').style.display = 'none';
							    	}
							    },panelHeight: 'auto'">
						</td>
					</tr>
					<tr id="tr" style="display: none;">
						<td>子项目:</td>
						<td>
							<input style="width: 100px;" id="projectitemchild" name="projectitemchild" class="easyui-combobox"
								editable="false"
								data-options="
							    valueField: 'id',
							    textField: 'name',
							    onSelect: function(rec){
									
							    },panelHeight: 'auto'">
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">发生日期：</td>
						<td>
							<input style="width: 100%;" id="urgentdate" name="urgentdate" class="easyui-datebox"
								data-options="editable:false" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">申请内容：</td>
						<td colspan="3">
							<input class="easyui-textbox" data-options="multiline:true" id="content" name="content"
								style="width: 100%; height: 70px;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">上传文件：</td>
						<td>
							<input class="easyui-filebox" data-options="prompt:'添加文件',buttonText:'选择附件',multiple:true" id="images"
								name="images" style="width: 100%;" />
						</td>
					</tr>
				</table>
			</form>
			<div style="text-align: right; " class="search-input" id="div2">
				<a href="#" class="d-btn btn-green" onclick="displayAtt()">查看附件</a>
			</div>
			<div style="text-align: center;" class="search-input" id="div1">
				<a href="#" class="d-btn btn-blue" onclick="savePro()">保存</a>
			</div>
		</div>
	</div>
	<div style="display: none">
		<div id="displayAtt" class="datagrid-toolbar"
			style="width: 98%; max-height: 80%; padding: 5px; text-align: center;"></div>
	</div>
</body>
<script th:inline="javascript">
	var currentPro = [[${urgentApply}]];

	$("#projectitemchild").combobox({ width:$("#width1").width()}); 
	$(document).ready(function() {
		if(currentPro != null){
			$("#projectitem").combobox("setValue", currentPro.item);
			if (currentPro.projectitemchild != null){
				$("#projectitemchild").combobox("setValue", currentPro.projectitemchild);
			}
			$("#urgentdate").datebox("setValue", formatDate(currentPro.urgentdate));
			$("#content").textbox("setValue", currentPro.content);
			if(currentPro.status != 6){
				document.getElementById('div1').style.display = 'none';
			}
		}else{
			document.getElementById('div2').style.display = 'none';
			currentPro = {};
		}
	});
	
	function savePro() {
		var formData = new FormData();
		if (currentPro.id){
			formData.append("id", currentPro.id);
		}
		formData.append("projectitemid", $("#projectitem").combobox('getValue'));
		if (currentPro.projectitemchild != null){
			formData.append("projectitemchildid", $("#projectitemchild").combobox('getValue'));
		}
		formData.append("content", $("#content").textbox('getValue'));
		formData.append("status", 6);
		formData.append("urgentdate", $("#urgentdate").datebox("getValue"));
		var images = document.getElementById("filebox_file_id_1").files;
		for(var i = 0;i < images.length;i++){
			formData.append('images', images[i]);
		}
		$.ajax({
			type : 'POST',
			url : "../urgentApplySave.do",
			cache : false,
			data : formData,
			processData : false,
			contentType : false,
			success : function(data) {
				if (data.success){
					$.messager.alert('保存工程联系单', '保存工程联系单成功', 'success');
				}else{
					$.messager.alert('保存工程联系单', '保存工程联系单失败', 'error');
				}
				window.setInterval("window.location.href=document.referrer", 2000);
			},
			dataType : "json"
		});
	}
	function displayAtt() {
		var row = currentPro;
		var tb = "";
		if(row.status == 6){
			$.each(row.attachments,function(i,val) {
				if(isImage(val.url)){
					tb+= '<img onclick="window.open(\'../'+val.url+ 
					'\',\'_blank\', \' toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no\');" '+
					'src="../'+val.url+'" style="width:100px;"><br><span>'+val.name+'</span><br>';
					tb+= '<a href="javascript:void(0)" class="btn-blue" onclick="remove('+val.id+')"><span>删除</span></a><br>'
				}else{
					tb+= '<a href="javascript:void(0)" onclick="attachmentFile('+val.id+',\''+val.name+
					'\')"><span>'+val.name+'</span></a><br>';
					tb+= '<a href="javascript:void(0)" class="btn-blue" onclick="remove('+val.id+')"><span>删除</span></a><br>'
				}
			});
		}else{
			$.each(row.attachments,function(i,val) {
				if(isImage(val.url)){
					tb+= '<img onclick="window.open(\'../'+val.url+ 
					'\',\'_blank\', \' toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no\');" '+
					'src="../'+val.url+'" style="width:100px;"><br><span>'+val.name+'</span><br>';
				}else{
					tb+= '<a href="javascript:void(0)" onclick="attachmentFile('+val.id+',\''+val.name+
					'\')"><span>'+val.name+'</span></a><br>';
				}
			});
		}
		$('#displayAtt').empty().html(tb);
		$('#displayAtt').dialog({
			title : '附件',
			resizable : true,
			closed : false,
			cache : false,
			modal : true
		});
	}
	function attachmentFile(id,name) {
		$.get('../attachmentFile.do?id=' + id, function(data) {
			blob = new Blob([ data ])
			var a = document.createElement('a');
			a.download = name;
			a.href = window.URL.createObjectURL(blob)
			a.click()
		})
	}
	function remove(id) {
		$.messager.confirm("操作提示", "您确定要执行操作吗？", function(data) {
			if (data){
				$.ajax({
					type : 'POST',
					url : "../attachmentDelete.do?id=" + id,
					contentType : "application/json",
					success : function(data) {
						if (data.success){
							$("#att" + id).remove();
							$.messager.alert('删除附件', '删除附件成功', 'success');
						}else{
							$.messager.alert('删除附件', '删除附件失败', 'error');
						}
						window.setInterval("location.reload()", 2000);
					},
					dataType : "json"
				});
			}
		});
	}
	function isImage(url) {
		var index= url.lastIndexOf(".");
		var ext = url.substr(index+1);
		return [ 'png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff' ,'ico'].
				indexOf(ext.toLowerCase()) !== -1;
	}
	function formatDate(time) {
		var date = new Date(time);
		var year = date.getFullYear(),month = date.getMonth() + 1, //月份是从0开始的
		day = date.getDate(),hour = date.getHours(),min = date.getMinutes(),sec = date.getSeconds();
		var newTime = year + '-' + month + '-' + day;
		return newTime;
	}
</script>

</html>